package com.hevs.samplewebapp.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TabPanel;
import com.hevs.samplewebapp.client.bean.Type;
import com.hevs.samplewebapp.client.screenComponents.ScreenComponentDoctorInformation;
import com.hevs.samplewebapp.client.screens.DoctorGraphWidget;
import com.hevs.samplewebapp.client.screens.DoctorStatisticsWidget;
import com.hevs.samplewebapp.client.screens.ManagePatientsWidget;

/**
 * This is the Doctor Panel
 * <p>The Goal is to provide different tools to the administrator the doctor.
 * The class is designed to host different tools with the possibility of adding new one easily. 
 * The Doctor will be able to manage medical data for its patients, see statistics and graphical representations.</p>
 * @author Matthias Hurni
 * @date 26.03.2012
 */
public class Doctor extends Composite
{
	// ------------------------------------------------------------
	// VARIABLES
	// ------------------------------------------------------------

	// Graphical Elements
	/**
	 * Doctor panel
	 * <p>Panel with multiple tabs, to make easy the adding of new features for administration.</p>
	 */
	private TabPanel tabPanel=new TabPanel();

	/**
	 * Managing Patients tool
	 * <p>Widget that contents tools to manage the medical data of different patients.</p>
	 * @see ManagePatientsWidget
	 */
	private ManagePatientsWidget managePatientsWidget=new ManagePatientsWidget();

	/**
	 * Graphical representation tool
	 * <p>Widget that contents tools to show a graphical representation of the medical data of different patients.</p>
	 * @see DoctorGraphWidget
	 */
	private DoctorGraphWidget doctorGraphWidget=new DoctorGraphWidget();

	/**
	 * Statistics tool
	 * <p>Widget that contents tools to display different statistics about the medical data of patients.</p>
	 * @see DoctorStatisticsWidget
	 */
	private DoctorStatisticsWidget doctorStatisticsWidget = new DoctorStatisticsWidget();

	/**
	 * Information panel
	 * <p>Widget that content main information to the doctor</p>
	 * @see ScreenComponentDoctorInformation
	 */
	private ScreenComponentDoctorInformation information;
	// URL
	/**
	 * Web Service URL
	 * <p>URL for getting the list of patients who granted this doctor</p>
	 */
	public static final String JSON_URL_PATIENTS=GWT.getModuleBaseURL()+"ws/granting/users";

	/**
	 * Web Service URL
	 * <p>URL for managing medical data of patients, it supports POST, PUT and GET</p>
	 */
	public static final String JSON_URL_MEDICALRECORD=GWT.getModuleBaseURL()+"ws/ressources/supervisor/medicalrecords/";
	/**
	 * Web Service URL
	 * <p>URL for deleting medical data of patients, it supports POST</p>
	 */
	public static final String JSON_URL_MEDICALRECORD_DEL=GWT.getModuleBaseURL()+"ws/delete/supervisor/medicalrecords/";

	//------------------------------------------------------------
	// CONSTRUCTORS
	//------------------------------------------------------------

	/**
	 * Constructor of the Doctors tools
	 * <p>It has a tab within the widget to manage medical data. 
	 * The first tab is about medical data of the patients who granted this doctor. 
	 * The second tab is about the statistics and the third is the graphical representation </p>
	 * @see ManagePatientsWidget
	 * @see DoctorGraphWidget
	 * @see DoctorStatisticsWidget
	 */
	public Doctor()
	{

		// Tab within the widget to manage medical data
		tabPanel.add(managePatientsWidget,"Patient Management");

		// Tab within the widget to display statistics
		tabPanel.add(doctorStatisticsWidget, "Statistics");

		// Tab within the widget to display graphical representation
		tabPanel.add(doctorGraphWidget,"Graphs");

		information = new ScreenComponentDoctorInformation();
		tabPanel.add(information, "Information");
		// Listener to refresh the content of the tab on click
		tabPanel.addSelectionHandler(new SelectionHandler<Integer>()
				{
			@Override
			public void onSelection(SelectionEvent<Integer> event)
			{
				if(event.getSelectedItem()==0){
					managePatientsWidget.refreshPatientCellTable();
					managePatientsWidget.clearMedTable();
				}

				if(event.getSelectedItem()==1)
					doctorStatisticsWidget.refreshPatientCellTable();

				if(event.getSelectedItem()==2)
				{
					doctorGraphWidget.refreshPatientCellTable();
					doctorGraphWidget.refreshGraph(Type.getFromInt(0));
				}
			}
				});
		
		tabPanel.selectTab(3);
		// refresh table
		managePatientsWidget.refreshPatientCellTable();

		// initialize the widget
		initWidget(tabPanel);
	}
}